*This file replicates regression results for Investment and Employment outcomes - Measurement Error.
*The code used in this file is adapted from Baker, Bloom, and Davis (2016) to generate results related to environmental policy uncertainty.

cap cd "panel_regressions"
clear all
use data/all_firm_data_updated
set more off

*sort acc to global company key and time
so gvkey year quarter
merge 1:1 gvkey year quarter using data/tobin_q_data, keepus(tobin_q) 
ren tobin_q tobin
drop if _m==2  
drop _m
so gvkey year quarter
merge 1:1 gvkey year quarter using data/cashflow_data, keepus(cashflow)
tab _m
drop if _m==2
drop _m

merge m:1 year quarter using data/modern_epu_quarterly 

foreach var in E P U EP EU PU EPU {
	gen lm_`var'=log(m_`var')
}
drop if _merge==2
drop _merge
merge m:1 year quarter using data/categories_yq 
gen yq=year+(quarter-1)/4
gen lepu=log(m_EPU)
gen leu=log(m_EU)

********************Updating/Fixing VIX
cap drop _merge
ren vix oldvix
so yq
merge m:1 yq using data/vix.dta,update
replace lvix=log(vix)
drop _m
merge m:1 yq using data/vxo_quarterly.dta,update
drop _m
*replace vix with vxo
replace vix=vxo if vix==.&vxo~=.
replace lvix=log(vix)

***************Add in Steve's intensity measure
so gvkey
merg m:1 gvkey using data/gvkey
drop if _m==2
drop _m
so cusip
merge m:1 cusip using data/steve10k_cusip
winsor2 jp,replace 

drop if _m==2
drop _m
ren jp jp 

merge m:1 year quarter using data/04_Env_Policy_indices_qtr

************************************************************************************************************************
***Here we make all of the necessary interactions for each intensity measure
************************************************************************************************************************
tsset ticker_code period_code

foreach var of varlist h_firm_sic_intens  {
	foreach var2 of varlist m_* lm_* vix lepu lvix fed fed_fore leu {
		qui gen `var2'_X_`var' = `var2'*`var'
		qui gen l1`var2'_X_`var' = l1.`var2'_X_`var'
		qui gen dl1`var2'_X_`var' = d.l1`var2'_X_`var'
	}
}

foreach var of varlist firm_intens sic_intens firm_sic_intens belo_intens jp {
	foreach var2 of varlist lepu fed {
		qui gen `var2'_X_`var' = `var2'*`var'
		qui gen l1`var2'_X_`var' = l1.`var2'_X_`var'
		qui gen dl1`var2'_X_`var' = d.l1`var2'_X_`var'
	}
}
gen lagppent=l.ppent

gen l1lepu=l1.lepu
gen dl1lepu=d.l1lepu
gen l1fed=l1.fed
gen dl1fed=d.l1fed
gen SIC2=int(sic4/100)

* Sectoral policy indices
gen env_firms=1 if inlist(sic3, 104, 109, 122, 131, 138, 241, 242, 491, 492) | inlist(SIC2, 1, 2, 37)
replace env_firms=0 if env_firms==.

foreach var in EPU_NB EP_NB {
replace `var'=0 if env_firms==0
}
*
drop _m
merge m:1 year quarter using data/A_DP_FP_HP_Quarter
drop _m

foreach var in dpu hpu fpu defense health fin_regulation{
gen `var'1=`var'
}

gen def_firms = inlist(sic3, 348, 381, 871) 
foreach var in defense dp dpu {
replace `var'=0 if def_firms==0
}

gen health_firms=(sic3>=800&sic3<=809)
foreach var in health hp hpu{
replace `var'=0 if health_firms==0
}

gen fin_firms= (sic3>=600&sic3<=679) 
foreach var in fin_regulation fp fpu{
replace `var'=0 if fin_firms==0
}

tsset ticker_code period_code

foreach var in EPU_NB EP_NB defense health fin_regulation dp dpu hp hpu fp fpu{
gen l`var'=log(`var')
gen dl`var'=d.l`var'
gen dl`var'_miss=(dl`var'==.)
replace dl`var'=0 if dl`var'_miss==1
}

label var iqr_fed_gov_forecast "Fed Exp. IQR"

***Compress
drop nom_fed_exp nom_gdp  vxo lvxo iqr_fed_gov_forecast ticker
egen avg_emp = mean(emp), by(ticker_code)
replace avg_emp=round(1000*avg_emp)

replace avg_emp=1 if avg_emp==0
egen mavg_emp=mean(avg_emp)
replace avg_emp=round(mavg_emp) if avg_emp==.
egen min_year = min(year), by(ticker_code )
gen age = year-min_year
drop *long obligatedamount atq xrd iq lrevtq firm_cap_intens firm_rd_intens sic2 
compress

gen inv_rate = capxy / l.ppentq

save data/itemp,replace
cap log close
cap log using invemp,replace t
tsset

********* Measurement Error test (Table A6)
qui reghdfe invest_ppe_rate dlEPU_NB dlEP_NB dldpu dldp dlhpu dlhp dlfpu dlfp dl1lepu_X_h_firm_sic_intens dl1fed_X_h_firm_sic_intens,  ab(ticker_code period_code) vce(cl ticker)
sum dpu1 hpu1 fpu1 defense1 health1 fin_regulation1 if e(sample)

 *For defense (>400) - (Q1 2003 i.e. 7964, 8012)
 *finance has large peaks (>400) - (Q3, 2008, Q4 2008, Q1 2009; Q3 2010 i.e. 8034, 8035, 8036, 8042, 8050)
 *health has large peaks (>400) - (Q4, 2010, Q3 2011, Q4 2012; i.e. 8043 8046 and 8051) 

 drop if inlist(period_code, 7964, 8012, 8034, 8035, 8036, 8042, 8050, 8043, 8046, 8051)
reghdfe invest_ppe_rate dlEPU_NB dlEP_NB dldpu dldp dlhpu dlhp dlfpu dlfp dl1lepu_X_h_firm_sic_intens dl1fed_X_h_firm_sic_intens,  ab(ticker_code period_code) vce(cl ticker)


************************************************************************************************************************************************************************
******************************************
***************EMPLOYMENT
*None of the annual PUs are >400. So no need to drop any period code.
